﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aspfm.Main
{
    [Serializable()]
    public class AspfmModel
    {
        public Hashtable Values { get { return _values; } }

        //保存对象动态定义的属性值
        private Hashtable _values = new Hashtable(StringComparer.OrdinalIgnoreCase);
        public AspfmModel() { }
        public AspfmModel(DataRow dr)
        {
            if (dr != null && dr.Table.Columns.Count > 0)
            {
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    string colname = dr.Table.Columns[i].ColumnName.ToUpper();
                    SetPropertyValue(colname, dr[colname]);
                }
            }
        }
        public AspfmModel(Hashtable ht)
        {
            this._values = ht;
        }
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public object GetPropertyValue(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                return null;
            if (_values.ContainsKey(propertyName) == true)
            {
                return _values[propertyName];
            }
            return null;
        }
        public string GetPropertyValueStr(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
                return "";
            if (_values.ContainsKey(propertyName) == true)
            {
                return _values[propertyName].ToString();
            }
            return "";
        }
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void SetPropertyValue(string propertyName, object value)
        {
            if (string.IsNullOrEmpty(propertyName))
                return;
            if (_values.ContainsKey(propertyName) == true)
            {
                _values[propertyName] = value;
            }
            else
            {
                _values.Add(propertyName, value);
            }
        }
    }
}
